
package com.shop.cloud.mall.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.common.security.entity.BaseUser;
import com.shop.cloud.common.security.util.SecurityUtils;
import com.shop.cloud.mall.admin.service.SupplierLogisticsAccountService;
import com.shop.cloud.mall.common.entity.ShopLogisticsAccount;
import com.shop.cloud.mall.admin.service.ShopLogisticsAccountService;
import com.shop.cloud.mall.common.entity.SupplierLogisticsAccount;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 店铺物流账号
 *
 * @author
 * @date 2023-04-18 14:09:16
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/shoplogisticsaccount")
@Tag(description = "shoplogisticsaccount", name = "店铺物流账号管理")
public class ShopLogisticsAccountController {

    private final ShopLogisticsAccountService shopLogisticsAccountService;
	private final SupplierLogisticsAccountService supplierLogisticsAccountService;

	/**
	 * 店铺物流账号list
	 * @param shopLogisticsAccount 店铺物流账号
	 * @return
	 */
	@Operation(summary = "店铺物流账号list")
	@GetMapping("/list")
	public R getList(ShopLogisticsAccount shopLogisticsAccount) {
		BaseUser baseUser = SecurityUtils.getUser();
		//供应商账号则查询供应商物流账号
		if(CommonConstants.USER_TYPE_3.equals(baseUser.getType())){
			return R.ok(supplierLogisticsAccountService.list(Wrappers.<SupplierLogisticsAccount>query().lambda()
					.select(SupplierLogisticsAccount::getId, SupplierLogisticsAccount::getAccountId, SupplierLogisticsAccount::getSupplierId)));
		}
		return R.ok(shopLogisticsAccountService.list(Wrappers.query(shopLogisticsAccount).lambda()
				.select(ShopLogisticsAccount::getId, ShopLogisticsAccount::getAccountId, ShopLogisticsAccount::getShopId)));
	}

    /**
     * 店铺物流账号分页列表
     * @param page 分页对象
     * @param shopLogisticsAccount 店铺物流账号
     * @return
     */
    @Operation(summary = "店铺物流账号分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ato.hasAuthority('mall:shoplogisticsaccount:index')")
    public R getPage(Page page, ShopLogisticsAccount shopLogisticsAccount) {
        return R.ok(shopLogisticsAccountService.page(page, Wrappers.query(shopLogisticsAccount)));
    }

    /**
     * 店铺物流账号查询
     * @param id
     * @return R
     */
    @Operation(summary = "店铺物流账号查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:shoplogisticsaccount:get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(shopLogisticsAccountService.getById(id));
    }

    /**
     * 店铺物流账号新增
     * @param shopLogisticsAccount 店铺物流账号
     * @return R
     */
    @Operation(summary = "店铺物流账号新增")
    @SysLog("新增店铺物流账号")
    @PostMapping
    @PreAuthorize("@ato.hasAuthority('mall:shoplogisticsaccount:add')")
    public R save(@RequestBody ShopLogisticsAccount shopLogisticsAccount) {
        return R.ok(shopLogisticsAccountService.save(shopLogisticsAccount));
    }

    /**
     * 店铺物流账号修改
     * @param shopLogisticsAccount 店铺物流账号
     * @return R
     */
    @Operation(summary = "店铺物流账号修改")
    @SysLog("修改店铺物流账号")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('mall:shoplogisticsaccount:edit')")
    public R updateById(@RequestBody ShopLogisticsAccount shopLogisticsAccount) {
        return R.ok(shopLogisticsAccountService.updateById(shopLogisticsAccount));
    }

    /**
     * 店铺物流账号删除
     * @param id
     * @return R
     */
    @Operation(summary = "店铺物流账号删除")
    @SysLog("删除店铺物流账号")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:shoplogisticsaccount:del')")
    public R removeById(@PathVariable String id) {
        return R.ok(shopLogisticsAccountService.removeById(id));
    }


}
